﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WiTeach.ClassManagement.Model;
using WiTeach.Tools;

namespace WiTeach.ClassManagement.DAL
{
    class ClassDAL
    {
        private static string tableName = "ta_lesson";


        /// <summary>
        /// 根据课程ID 获取对应的课时列表信息
        /// </summary>
        /// <author>Light</author>
        /// <param name="Lesson_ID">课程ID</param>
        /// <returns></returns>
        /// 
        public static List<Class> getClassListByID(int Lesson_ID)
        {
            List<Class> classList = new List<Class>();
            string sql = "select * from " + tableName + " where course_id = " + Lesson_ID;
            DataTable classTable = SqlHelper.ExecuteDataTable(sql);
            return classToList(classTable);
        }

        #region 数据分页相关
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <author>Light</author>
        /// <param name="pageNow"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<Class> getCurrentPagingList(int LessonID, int pageNow, int pageSize, int pageCount)
        {
            if (pageCount == 0) return null;
            string sql = "select * from " + tableName + " where course_id = " + LessonID + " limit " + (pageNow % pageCount) * pageSize + ", " + pageSize + "";
            DataTable dataTable = SqlHelper.ExecuteDataTable(sql);
            return classToList(dataTable);
        }

        /// <summary>
        /// 课程模型数据转集合
        /// </summary>
        /// <author>Light</author>
        /// <param name="dataTable"></param>
        /// <returns>模型集合</returns>
        public static List<Class> classToList(DataTable dataTable)
        {
            List<Class> classList = new List<Class>();
            if (dataTable == null)
                return null;
            foreach (DataRow dt in dataTable.Rows)
            {
                Class classInfo = new Class();
                classInfo.ClassHour_ID = (int)dt["id"];
                classInfo.Lesson_ID = (int)dt["course_id"];
                classInfo.ClassHour_Name = (string)dt["name"];
                classList.Add(classInfo);
            }
            return classList;
        }

        /// <summary>
        /// 获取数据表中数据条数
        /// </summary>
        /// <returns></returns>
        public static int getCountByName()
        {
            string strSql = "select count(*) from " + tableName;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(strSql));
        }

        #endregion

        public static string getClassNameByID(int lessonID)
        {
            string sql = "select name as  Lesson_Name from "+tableName+" where course_id = " + lessonID;
            return (string)SqlHelper.ExecuteScalar(sql);
        }
        /// <summary>
        /// 获取制定课程对应的课时列表数量
        /// </summary>
        /// <param name="lessonID"></param>
        /// <returns></returns>
        public static int getCountByID(int lessonID)
        {
            string strSql = "select count(*) from " + tableName + " where course_id = " + lessonID;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(strSql)); 
        }


        /// <summary>
        /// 获取课程列表总条数
        /// </summary>
        /// <returns></returns>
        public static int getClassCount()
        {
            return BaseDAL.getTotalCount(tableName);
        }

    }
}
